package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduChapter;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.vo.CourseInfo;
import com.atguigu.serviceedu.entity.vo.CoursePublishVo;
import com.atguigu.serviceedu.entity.vo.CourseQuery;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduCourseDescriptionService;
import com.atguigu.serviceedu.service.EduCourseService;
import com.atguigu.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author JY
 * @since 2021-01-19
 */
@Api(description="课程管理")
@RestController
@RequestMapping("/serviceedu/course")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduVideoService videoService;

    //分页+条件查询
    @PostMapping("pageCondition/{page}/{limit}")
    public R pageCondition(@PathVariable Long page, @PathVariable Long limit,
                           @RequestBody(required = false)CourseQuery courseQuery){
        Page<EduCourse> pageParam = new Page<>(page,limit);

        //构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();

        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(teacherId)){
            wrapper.eq("teacher_id",teacherId);
        }
        if(!StringUtils.isEmpty(subjectParentId)){
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        if(!StringUtils.isEmpty(subjectId)){
            wrapper.eq("subject_id",subjectId);
        }

        //按创建时间时间降序排序，最新的放前面
        wrapper.orderByDesc("gmt_create");

        courseService.page(pageParam,wrapper);

        long total = pageParam.getTotal();
        List<EduCourse> records = pageParam.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    //删除课程
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        //删除对应的小节
        videoService.deleteByCourseId(id);
        //删除对应的章节
        chapterService.deleteByCourseId(id);
        //删除对应的描述
        courseDescriptionService.removeById(id);
        //删除对应的课程
        boolean flag = courseService.removeById(id);

        return flag?R.ok():R.error().message("删除失败");
    }


    //增加课程
    @PostMapping("save")
    public R save(@RequestBody CourseInfo courseInfo){
        //为了方便增加章节，返回courseId
        String id = courseService.saveCourseInfo(courseInfo);
        return R.ok().data("courseId",id);
    }

    //得到课程基本信息CourseInfo
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfo courseInfo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo",courseInfo);
    }

    //更新课程基本信息
    @PostMapping("update")
    public R update(@RequestBody CourseInfo courseInfo){
        courseService.updateInfo(courseInfo);
        return R.ok();
    }

    //根据ID获取课程发布信息
    @GetMapping("getCoursePublishVoById/{id}")
    public R getCoursePublishVoById(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        return R.ok().data("item",coursePublishVo);
    }

    //根据id发布课程
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");
        courseService.updateById(course);
        return R.ok();
    }
}

